package top.infopub.mgr.adm.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.adm.dao.BorrowRecordDao;
import top.infopub.mgr.adm.dao.BorrowRecordListDao;
import top.infopub.mgr.adm.domain.BorrowRecordDomain;
import top.infopub.model.adm.BorrowRecord;
import top.infopub.model.adm.BorrowRecordList;
import top.infopub.mgr.adm.service.BorrowRecordService;
import top.infopub.mgr.adm.vo.BorrowRecordVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;

@Service
public class BorrowRecordServiceImpl extends GenericServiceImpl<BorrowRecord, String> implements
		BorrowRecordService {

	@Autowired
	private BorrowRecordDao borrowRecordDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Autowired
	private BorrowRecordListDao borrowRecordListDao;
	
	@Override
	public GenericDao<BorrowRecord, String> getDao() {
		return borrowRecordDao;
	}

    @Override
    public Page<BorrowRecordDomain> selectByDomainWhere(Page<BorrowRecordDomain> page,
                                                        BorrowRecordVo borrowRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BorrowRecordDomain>)borrowRecordDao.selectByDomainWhere(borrowRecordVo);
    }

    @Override
    public Page<BorrowRecordDomain> selectMyCheckByDomainWhere(Page<BorrowRecordDomain> page,
                                                               BorrowRecordVo borrowRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BorrowRecordDomain>)borrowRecordDao.selectMyCheckByDomainWhere(borrowRecordVo);
    }

    @Override
    public BorrowRecordVo queryById(String id) {
        return borrowRecordDao.queryById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBorrowRecord(BorrowRecord borrowRecord,
                                 List<BorrowRecordList> borrowRecordLists) {
        
        borrowRecordDao.insertById(borrowRecord);
        
        if(CollectionUtils.isNotEmpty(borrowRecordLists)){
            borrowRecordListDao.insertByBatch(borrowRecordLists);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.ADM_BORROW_RECORD;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, borrowRecord.getId(), borrowRecord.getProcessId(), null);
        
    }
}